// coding: utf-8
// @Author : lryself
// @Date : 2021/1/13 10:49
// @Software: GoLand

package main

import (
	"context"
	"fmt"
	"github.com/chromedp/chromedp"
	"log"
	"time"
)

func main() {
	urlIndex := "https://app.ncepu.edu.cn/ncov/wap/default/index"
	userNumber := ""
	password := ""
	chromePath := "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	options := []chromedp.ExecAllocatorOption{
		// false意味展示浏览器窗口，默认true
		chromedp.ExecPath(chromePath),
		chromedp.Flag("headless", false),
		chromedp.Flag("hide-scrollbars", false),
		chromedp.Flag("mute-audio", false),
		chromedp.UserAgent(`Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36`),
	}
	options = append(chromedp.DefaultExecAllocatorOptions[:], options...)
	c, cc := chromedp.NewExecAllocator(ctx, options...)
	defer cc()

	ctx, cancel = chromedp.NewContext(c)
	defer cancel()

	ctx, cancel = context.WithTimeout(ctx, 1*time.Minute)
	defer cancel()

	err := chromedp.Run(ctx,
		//访问打开url页面
		chromedp.Navigate(urlIndex),
		// 等待加载完成
		chromedp.WaitVisible(`#app > div.btn`),
		//输入学号
		chromedp.SendKeys(`#app > div.content > div:nth-child(1) > input[type=text]`, userNumber, chromedp.ByID),
		//输入密码
		chromedp.SendKeys(`#app > div.content > div:nth-child(2) > input[type=password]`, password, chromedp.ByID),
		//点击登录
		chromedp.Click(`#app > div.btn`, chromedp.NodeVisible),

		//开始填报
		//等待加载完成
		chromedp.WaitVisible(`body > div.item-buydate.form-detail2 > div > div > section > div.list-box > div > a`),
		//是否在校
		chromedp.Click(`body > div.item-buydate.form-detail2 > div > div > section > div.form > ul > li:nth-child(4) > div > div > div:nth-child(2) > span.active`, chromedp.NodeVisible),

		//所在地点
		chromedp.Click(`body > div.item-buydate.form-detail2 > div > div > section > div.form > ul > li:nth-child(5) > div > div > div:nth-child(1) > span.active`, chromedp.NodeVisible),

		//所在地点
		chromedp.Click(`body > div.item-buydate.form-detail2 > div > div > section > div.form > ul > li:nth-child(8) > div > input[type=text]`, chromedp.NodeVisible),

		//是否在中高风险地区
		chromedp.Click(`body > div.item-buydate.form-detail2 > div > div > section > div.form > ul > li:nth-child(9) > div > div > div:nth-child(2) > span.active`, chromedp.NodeVisible),

		//今日体温范围
		chromedp.Click(`body > div.item-buydate.form-detail2 > div > div > section > div.form > ul > li:nth-child(10) > div > div > div:nth-child(2) > span.active`, chromedp.NodeVisible),

		//您属于以下的哪种情况？
		chromedp.Click(`body > div.item-buydate.form-detail2 > div > div > section > div.form > ul > li:nth-child(11) > div > div > div:nth-child(1) > span.active`, chromedp.NodeVisible),

		//今日是否出现发热、乏力、干咳、呼吸困难等症状
		chromedp.Click(`body > div.item-buydate.form-detail2 > div > div > section > div.form > ul > li:nth-child(12) > div > div > div:nth-child(2) > span.active`, chromedp.NodeVisible),

		//今日是否接触无症状感染/疑似/确诊人群
		chromedp.Click(`body > div.item-buydate.form-detail2 > div > div > section > div.form > ul > li:nth-child(13) > div > div > div:nth-child(2) > span.active`, chromedp.NodeVisible),

		//今日是否接触密接人员
		chromedp.Click(`body > div.item-buydate.form-detail2 > div > div > section > div.form > ul > li:nth-child(14) > div > div > div:nth-child(2) > span.active`, chromedp.NodeVisible),

		//近14日内本人/共同居住者是否去过疫情发生场所（市场、单位、小区等）或与场所人员有过密切接触
		chromedp.Click(`body > div.item-buydate.form-detail2 > div > div > section > div.form > ul > li:nth-child(15) > div > div > div:nth-child(2) > span.active`, chromedp.NodeVisible),

		//是否处于观察期
		chromedp.Click(`body > div.item-buydate.form-detail2 > div > div > section > div.form > ul > li:nth-child(16) > div > div > div:nth-child(2) > span.active`, chromedp.NodeVisible),

		//所在社区是否有确诊病例
		chromedp.Click(`body > div.item-buydate.form-detail2 > div > div > section > div.form > ul > li:nth-child(17) > div > div > div:nth-child(2) > span.active`, chromedp.NodeVisible),

		//是否有任何与疫情相关的， 值得注意的情况
		chromedp.Click(`body > div.item-buydate.form-detail2 > div > div > section > div.form > ul > li:nth-child(18) > div > div > div:nth-child(2) > span.active`, chromedp.NodeVisible),

		//等待加载完成
		chromedp.Sleep(1*time.Second),

		//提交
		chromedp.Click(`body > div.item-buydate.form-detail2 > div > div > section > div.list-box > div > a`, chromedp.NodeVisible),

	//提交
	//chromedp.Click(`#wapat > div > div.wapat-btn-box > div`, chromedp.NodeVisible),

	)
	if err != nil {
		log.Fatal(err)
	} else {
		fmt.Println("填报完成！")
		fmt.Println("输入任意键结束程序。。。")
		_, _ = fmt.Scanf("%s")
	}

}
